package com.qfjy.project.meeting;

import com.qfjy.common.utils.crypto.AESUtils;
import com.qfjy.common.utils.result.ResultCode;
import com.qfjy.common.utils.result.ResultJson;
import com.qfjy.entity.po.MeetingGrab;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.entity.vo.MeetingGrabVO;
import com.qfjy.service.MeetingGrabService;
import com.qfjy.service.MeetingPubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName MeetingGrabController
 * @Description TODO  MeetingGrab抢单
 * @Author guoweixin
 * @Date 2022/12/7
 * @Version 1.0
 */
@Controller
@Slf4j
@RequestMapping("meetingGrab")
@Api(tags = "会议抢单模块")
public class MeetingGrabController {
    /**会议发单业务逻辑*/
    @Autowired
    private MeetingPubService meetingPubService;


    @Autowired
    private MeetingGrabService meetingGrabService;

    /**
     * 会议抢单 可抢单列表
     */
    @ApiOperation(value = "我的可抢单列表功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name="uid",value = "抢单人id",defaultValue = "328"),
            @ApiImplicitParam(name="tid",value = "课题类别id",defaultValue = "0")
    })
    @ResponseBody
    @PostMapping("grabListByUid")
    public ResultJson grabListByUid(@RequestParam("uid") final String uid,
                                    @RequestParam("tid") final int tid){
        String uidStr=null;
        try {
             uidStr = AESUtils.decryptMethod(uid);
        }catch (Exception ex){
            log.info("前端用户恶意更改地址:{}",ex.getMessage());
            return new ResultJson(null,ResultCode.PARAMS_ERROR);
        }

       List<MeetingPub> list=  meetingPubService.grabListByUid(Integer.parseInt(uidStr),tid);

       if(list.size()==0){
           return new ResultJson(null, ResultCode.NOT_DATA);
       }

        return new ResultJson(list,ResultCode.SUCCESS);
    }

    /**
     * 可抢单按钮进入页面   meetingGrab/addMeetingGrabToPage
     */
    @GetMapping("addMeetingGrabToPage")
    public String addMeetingGrabToPage(HttpServletRequest request){

       String uid= request.getParameter("uid");
        String pid= request.getParameter("pid");

        request.setAttribute("uid",uid);
        request.setAttribute("pid",pid);
        return "meetingGrab/addMeetingGrab";
    }

    /**
     * 添加 会议抢单
     */
    @PostMapping("addMeetingGrab")
    @ResponseBody
    public ResultJson addMeetingGrab(MeetingGrabVO meetingGrabVO){


        String uidStr= meetingGrabVO.getUid();
        uidStr=AESUtils.decryptMethod(uidStr);

        MeetingGrab meetingGrab=new MeetingGrab();
        meetingGrab.setPid(meetingGrabVO.getPid());
        meetingGrab.setRemark(meetingGrabVO.getRemark());
        meetingGrab.setUid(Integer.parseInt(uidStr));

        int num=0;
        try {
           num = meetingGrabService.insertSelective(meetingGrab);
      }catch (Exception ex){
          return new ResultJson(null,ResultCode.FAIL);
      }
       log.info("会议抢单成功{}",num);
        return new ResultJson(num,ResultCode.SUCCESS);
    }

    /**
     * 会议抢单-》可抢单列表-》我的抢单列表
     */
    @GetMapping("grabListByMyUid")
    @ResponseBody
    @ApiOperation(value = "根据抢单人id查询抢单列表信息")
    public ResultJson grabListByMyUid(@RequestParam("uid")  String uid){
        try {
            uid = AESUtils.decryptMethod(uid);
        }catch (Exception ex){
            return  new ResultJson(null,ResultCode.PARAMS_ERROR);
        }
       List<MeetingPub> list=meetingPubService.grabListMyByUid(Integer.parseInt(uid));

        return new ResultJson(list,ResultCode.SUCCESS);
    }
}
